package com.example.mouther_bg.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mouther_bg.Bean.User;
import com.example.mouther_bg.service.userService;
import com.example.mouther_bg.utils.JWTUtils;
import com.example.mouther_bg.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
public class userController {
    @Autowired
    private userService userService;
    @GetMapping("/public/login")
    @CrossOrigin(origins = "*")
    public Result login(String userName,String password){
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        Wrapper.eq("user_name",userName);
        Wrapper.and(wrapper -> wrapper.eq("pass_word", password));
        User users = userService.getOne(Wrapper);
        if (users != null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("username",userName);
            String token = JWTUtils.getToken(map);
            return Result.tokenSucc(token);
        }
        return Result.err();
    }
    @GetMapping("/queryUserById")
    @CrossOrigin(origins = "*")
    public Result queryUserById(int id){
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        Wrapper.eq("id", id);
        User user = userService.getOne(Wrapper);
        return Result.succ(user);
    }
    @GetMapping("/public/phone")
    @CrossOrigin
    public  Result phone (String phone,String code){
        if(phone.equals("17314351276") && code.equals("112233")) {
            QueryWrapper<User> Wrapper = new QueryWrapper<>();
            Wrapper.eq("phone", phone);
            User user = userService.getOne(Wrapper);
            String userName = user.getUserName();
            if (user != null) {
                HashMap<String, String> map = new HashMap<>();
                map.put("username", userName);
                String token = JWTUtils.getToken(map);
                return Result.tokenSucc(token);
            }
            return Result.tokenErr(403, "权限认证失败");
        }
        return Result.err();
    }

    @PostMapping("/user/queryUser")
    @CrossOrigin(origins = "*")
    public Result queryUser(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,HEAD,PUT,PATCH");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, token");
        DecodedJWT verify = JWTUtils.verify(request.getHeader("Authorization"));
        String username = verify.getClaim("username").asString();
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        Wrapper.eq("user_name",username);
        User userDB = userService.getOne(Wrapper);
        return Result.succ(userDB);
    }
}
